import instance from './request.js';


export const $ = document.querySelector.bind(document);
// const $$ = document.querySelectorAll.bind(document);
export const $$ = (selector) => Array.from(document.querySelectorAll(selector));

export function sendAJAX(url) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.responseType = 'json';
        xhr.withCredentials = true;
        xhr.open("GET", url, true);
        xhr.send();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    resolve(xhr.response);
                } else {
                    reject(xhr.status);
                }
            }
        }
    })
}

const PromptBox = document.querySelector('.PromptBox');
export function tooltipBox(icon, information, bool) {
    var div = document.createElement('div');//创建一个div节点
    var span = document.createElement('span');
    div.innerHTML = information;//加内容信息
    span.innerHTML = icon;
    div.classList.add('prompt');//加样式
    if (bool == 1) {
        span.classList.add('iconstyle');
    } else if (bool == 0) {
        span.classList.add('iconstylee');
    }
    div.insertBefore(span, div.children[0]);
    PromptBox.insertBefore(div, PromptBox.children[0]);//放进最上面的盒子里

    //放进里面1毫秒后 发生改变才会过度
    setTimeout(() => {
        // span.classList.add('shown');
        div.classList.add('shown');
    }, 5);
    //过3秒后删除 发生改变
    setTimeout(() => {
        // span.classList.remove('shown');
        div.classList.remove('shown');
    }, 1500)
    //过3.5秒后删除弹窗
    setTimeout(() => {
        // span.remove();
        div.remove();
    }, 2000)
}

document.addEventListener('selectstart', function (event) {
    event.preventDefault(); // 阻止选择文字的默认行为
});


export function navbox() {
    //nav右边的显示框
    $('.nav-r-2').addEventListener('click', function () {
        $('.nav_box').style.display = 'block';
    })
    document.addEventListener("click", function (event) {
        // 如果点击的是输入框或其子元素，则不做任何处理
        if (!(event.target === $('.nav-r-2') || $('.nav-r-2').contains(event.target) || event.target === $('.nav_box') || $('.nav_box').contains(event.target))) {
            $('.nav_box').style.display = 'none';
        }
    });
}

export function SaferHTML(templateData) {
    let s = templateData[0];
    for (let i = 1; i < arguments.length; i++) {
        let arg = String(arguments[i]);

        s += arg.replace(/&/g, "&amp;")
            .replace(/</g, "&lt;")
            .replace(/>/g, "&gt;");

        s += templateData[i];
    }
    return s;
}



let userid = localStorage.getItem('userid');
export function avatarimg() {
    instance.get(`/user/${userid}`, {
        headers: {
            'Authorization': window.localStorage.getItem('token'),
            'Content-Type': 'application/json',
        }
    }).then(response => {
        let userarr = response.data.data;
        console.log(userarr);
        if (userarr.avatarUrl) {
            $('.nav-r-2-svg2 img').src = userarr.avatarUrl;
        } else {
            $('.nav-r-2-svg2 img').src = 'https://z1.muscache.cn/defaults/user_pic-50x50.png?v=3&im_w=240';
        }
    })
}




// 防抖功能函数，接受传参
export function debounce(fn) {
    let timeout = null;
    return function () {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            fn.call(this, [...arguments]); // 将 arguments 转换为数组
        }, 1000);
    };
}


